﻿using System.Linq;
using System.Text;
using SharePointOptimizer.Model;
using SharePointOptimizer.BL.DataAnalysis;
using System;
using AG.Utilities;
using System;
namespace SharePointOptimizer.BL.DataAnalysis.IssueTypes
{
    public class FarmBackupCheck : IssueTypeCheckBase
    {

        public FarmBackupCheck(int issueTypeId)
            : base(issueTypeId)
        {

        }

        public override void Check()
        {
try{

            // Get all Service instance that has ServiceTypeID 1
            Farm farm = dc.Farms.FirstOrDefault();


            if (farm != null)
            {

                Service service = farm.Services.SingleOrDefault(s => s.TypeName == "Microsoft SharePoint Foundation Web Application");


                if (service != null)
                {

                    var QryLists = from b in dc.Backups
                                   where b.TopComponent == "Farm"
                                   orderby b.EndTime descending
                                   select new { b.EndTime, b.TopComponentID };

                    foreach (var backup in QryLists)
                    {

                        TimeSpan diffrence = DateTime.Now.Subtract(backup.EndTime.Value);
                        if (diffrence.Days > 1)
                        {
                            Issue issue = new Issue();
                            issue.IssueType = issueType;
                            issue.TargetId = backup.TopComponentID;
                            issue.Description = issueType.Description.Replace("{backupdate}", backup.EndTime.Value.Date.ToString());
                            dc.Issues.InsertOnSubmit(issue);
                        }
                        break;
                    }


                    dc.SubmitChanges();
                }

            }
}
    catch (Exception ex)
            {
                Logger.Error(ex);
            }
            

        }

    }

}
